<?php
namespace app\work\controller;
use app\work\logic\NoticeLogic;

/**
 * 通知公告api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/13
 * Time: 16:27
 */
class Notice extends Base {

    public function __construct(){
        parent::__construct();
        $this->n_logic = new NoticeLogic();
    }

    /**
     * 新增通知公告
     * @return \think\response\Json
     */
    public function ajax_add_notice(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->n_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增通知公告规则
        $r = $this->n_logic->check_add_notice_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wrn_data = [];
        $wrn_data['openid'] = $u_detail['openid'];
        $wrn_data['name'] = trim($params['name']);
        $wrn_data['content'] = trim($params['content']);
        $wrn_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
        $wrnid = model('work_record_notice')->insertGetId($wrn_data);
        if($wrnid){
            //同步发送工作圈
            $r = $this->n_logic->sync_send_wcm($u_detail['openid'],$params);

            //更新发布通知公告积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_rn_integral($wrnid);

            //生成抄送
            $c = new \app\common\controller\Copy();
            if(!empty($params['copy_openid'])){
                //生成抄送（未抄送）
                $r = $c->gen_copy_not($u_detail['openid'],$wrnid,6,$params['copy_openid']);
                //修改抄送（已抄送）
                $r = $c->save_copy_open($u_detail['openid'],$wrnid,6);
            }

            return json($this->n_logic->get_response(1,'提交成功'));
        }
        return json($this->n_logic->get_response(1,'提交失败'));
    }
}
